package com.stock.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.stock.pojo.vo.StockInfoConfig;
import com.stock.utils.IdWorker;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @description : 定义公共的配置类
 */

@EnableConfigurationProperties(StockInfoConfig.class)
@Configuration
public class CommonConfig {
    /**
     * 定义密码加密匹配器bean
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        /**
         * 密码加密器
         * BCryptPasswordEncoder方法采用SHA-256对密码进行加密
         * @return
         */
        return new BCryptPasswordEncoder();
    }



        /**
         * 配置id生成器bean
         * 虽然代码在common中，但是哪个模块下用的就要配置哪个模块
         * @return
         */
        @Bean
        public IdWorker idWorker(){
            //基于运维人员对机房和机器的编号规划自行约定
            /*
                参数一：机器ID
                参数二：机房ID
             */
            return new IdWorker(1l,2l);
        }

    /**
     * 本地缓存
     * @return
     */
    @Bean
    public Cache<String,Object> caffeineCache(){
        Cache<String, Object> cache = Caffeine
                .newBuilder()
                .maximumSize(200)//设置缓存数量上限
//                .expireAfterAccess(1, TimeUnit.SECONDS)//访问1秒后删除
//                .expireAfterWrite(1,TimeUnit.SECONDS)//写入1秒后删除
                .initialCapacity(100)// 初始的缓存空间大小
                .recordStats()//开启统计
                .build();
        return cache;
    }

}
